﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

public partial class admin_subscriberlist : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = SiteName + " - Contact List [ Admin ]";
        LoadScripts();
    }

    private void LoadScripts()
    {
        deleteAllButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
    }

    protected void subscribeList_RowCommand(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "editCommand")
        {
            Response.Redirect("subscribedetail.aspx?id=" + e.CommandArgument.ToString() + "&mode=" +
                FormViewMode.Edit.ToString());
        }
        else if (e.CommandName == "deleteCommand")
            subscribeList.DeleteRow(Convert.ToInt32(e.CommandArgument)); //command argument contains row index
    }

    protected void subscribeList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox selectAll = (CheckBox)e.Row.FindControl("selectAll");
            selectAll.Attributes.Add("onclick", "SelectAll('" + subscribeList.ClientID + "','" + selectAll.ClientID + "')");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Int32 id = (Int32)subscribeList.DataKeys[e.Row.RowIndex].Value;

            ImageButton deleteButton = (ImageButton)e.Row.FindControl("deleteButton");
            deleteButton.AlternateText = "delete";
            deleteButton.CommandArgument = e.Row.RowIndex.ToString();
            deleteButton.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
        }
    }

    protected void addNewButton_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("subscribedetail.aspx?mode=Insert");
    }

    protected void deleteAllButton_Click(object sender, ImageClickEventArgs e)
    {
        GridViewRowCollection subscribeListRows = subscribeList.Rows;
        List<int> idToDelete = new List<int>();

        foreach (GridViewRow row in subscribeListRows)
        {
            CheckBox rowSelection = (CheckBox)row.FindControl("rowSelect");

            if (rowSelection.Checked)
            {
                idToDelete.Add((int)subscribeList.DataKeys[row.RowIndex].Value);
            }
        }

        if (Pheonix.Fashionzoomer.Entity.SqlServer2000.Subscribe.DeleteSubscribeList(idToDelete))
        {
            subscribeList.DataBind();
        }
        else
        {

        }
    }
}
